package com.boom.framework.manager;

import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.boom.common.utils.Threads;
import com.boom.common.utils.spring.SpringUtils;

/**
 * 异步任务管理器
 *
 * @author boom
 */
public class AsyncManager {

	/**
	 * 异步操作任务调度线程池
	 */
	private final ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");

	/**
	 * 单例模式
	 */
	private AsyncManager() {
	}

	private static final AsyncManager ME = new AsyncManager();

	public static AsyncManager me() {
		return ME;
	}

	/**
	 * 执行任务
	 *
	 * @param task 任务
	 */
	public void execute(TimerTask task) {
		executor.schedule(task, 10, TimeUnit.MILLISECONDS);
	}

	/**
	 * 停止任务线程池
	 */
	public void shutdown() {
		Threads.shutdownAndAwaitTermination(executor);
	}
}
